package hello.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import hello.entity.ExcelTest;
import hello.mapper.ExcelTestMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "Excel相关")
@RestController
@RequestMapping("/excel")
public class ExcelController {


    @Autowired
    ExcelTestMapper excelTestMapper;

    @ApiOperation(value = "Excel导出")
    @GetMapping("")
    public void get(HttpServletResponse res) throws IOException {
        String fileName = "result_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + ".xlsx";
        // 讲请求结果转换成流
//        res.setHeader("content-type", "application/octet-stream");
        res.setContentType("application/octet-stream");
        res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        OutputStream os = res.getOutputStream();
        List<ExcelTest> records = excelTestMapper.selectList(null);
        List<ExcelTest> addList = new ArrayList<>();

        ExcelTest excel = new ExcelTest();
        excel.setName("马成天江");
        excel.setAge("28");
        excel.setGender("男");
        
        addList.add(excel);
        records.forEach(x -> {
            ExcelTest recordExcelBean = new ExcelTest();
            recordExcelBean.setAge(x.getAge());
            recordExcelBean.setGender(x.getGender());
            recordExcelBean.setName(x.getName());
            addList.add(recordExcelBean);
        });
        Workbook workbook = ExcelExportUtil.exportBigExcel(new ExportParams("第七课", "表1"),
                ExcelTest.class, addList);
        workbook.write(os);
        os.close();
        addList.clear();
    }

    @ApiOperation("导入")
    @PostMapping("")
    public void post(MultipartFile file) throws Exception {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        List<ExcelTest> list = ExcelImportUtil.importExcel(file.getInputStream(), ExcelTest.class, params);

        list.forEach(x -> {
            ExcelTest excel = new ExcelTest();
            excel.setAge(x.getAge());
            excel.setGender(x.getGender());
            excel.setName(x.getName());
            excel.insert();
        });

        
    }


}